<?php
namespace Kuoqi\Api\Weixin\Applet;

use Kuoqi\Api\Weixin\Tools;
use Kuoqi\Api\Weixin\Config;

/**
 * Class Fans
 * @package Kuoqi\Api\Weixin\Applet
 * User kuoqi
 * Datetime 2022/3/2 16:21
 * Remark 偏向业务模块
 */
class Fans{

    protected $code = null;

    protected $link = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';

    protected $method = 'get';

    /**
     * @param $code
     * User kuoqi
     * Datetime 2022/3/3 9:15
     * Remark 设置code
     */
    public function setCode($code){
        $this->code = $code;
    }

    /**
     * @return array
     * User kuoqi
     * Datetime 2022/3/3 9:15
     * Remark 登录模块
     */
    public function login(){
        $link = sprintf($this->link
            ,Config::$applet_appid
            ,Config::$applet_secret
            ,$this->code
        );
        $rs = Tools::sendTo($link,$this->method,array(),'json',6);
        return Tools::response($rs);
    }

    /**
     * @param $code 微信小程序获取到的
     * @param $password
     * @param $iv
     * @return array
     * User kuoqi
     * Datetime 2022/3/3 10:24
     * Remark 获取手机号码
     */
    public function getPhonenum($encrypted_data,$session_key,$iv){
        $stream = \Kuoqi\Openssl::decode($encrypted_data,'',base64_decode($session_key),base64_decode($iv));
        if($stream){
            return Tools::response(json_decode($stream,true));
        }else{
            return Tools::response(array('errcode'=>'9999','errmsg'=>'解密失败'));
        }
    }

    /**
     * 解码加密字符串
     * @param $encrypted_data
     * @param $session_key
     * @param $iv
     * @return array
     */
    public function decode($encrypted_data,$session_key,$iv){
        $stream = \Kuoqi\Openssl::decode($encrypted_data,'',base64_decode($session_key),base64_decode($iv));
        if($stream){
            return Tools::response(json_decode($stream,true));
        }else{
            return Tools::response(array('errcode'=>'9999','errmsg'=>'解密失败'));

        }
    }



}